package com.lvyq.service.impl;

import com.lvyq.model.bag.MyBag;
import com.lvyq.model.receive.ReceiveMsg;
import com.lvyq.model.shop.ShopGoods;
import com.lvyq.model.user.JueWei;
import com.lvyq.model.user.User;
import com.lvyq.repository.JueWeiRepository;
import com.lvyq.repository.MyBagRepository;
import com.lvyq.repository.UserRepository;
import com.lvyq.service.UserService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: weg
 * @description:
 * @author: lvyq
 * @create: 2023-03-17 20:25
 **/
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserRepository userRepository;

    @Resource
    private JueWeiRepository jueWeiRepository;

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private MyBagRepository myBagRepository;



    /** 
    * @Description: 用户存储
    * @params:  * @param openId
    * @return: 
    * @Author: lvyq
    * @Date: 2023/3/17 20:28
    */
    @Override
    public void saveUser(String openId) {
        //查询
        User user = userRepository.findByOpenId(openId);
        if (user==null){
            User user1=new User();
            user1.setOpenId(openId);
            user1.setNickName("小窝"+openId);
            user1.setJingyan(0);
            user1.setJuewei("骑士");
            user1.setIsSignIn(false);
            user1.setDengJi(0);
            user1.setJinbi(0);
            user1.setMeili(0);
            user1.setCreateTime(new Date());
            userRepository.save(user1);
        }

    }

    /** 
    * @Description: 个人相关指令
    * @params:  * @param replyContent
    * @param receiveMsg
    * @return: 
    * @Author: lvyq
    * @Date: 2023/3/23 18:31
    */
    @Override
    public String orderReply(String replyContent, ReceiveMsg receiveMsg) {
        //个人中心查询
        if (receiveMsg.getContent().equals("个人中心")){
            replyContent=userInfoReply(replyContent,receiveMsg);
        }
        //签到
        if (receiveMsg.getContent().equals("签到")){
            replyContent=signReply(replyContent,receiveMsg);
        }
        if (receiveMsg.getContent().startsWith("修改昵称#")){
            replyContent=nickNameReply(replyContent,receiveMsg);
        }
        //我的背包
        if (receiveMsg.getContent().equals("我的背包")){
            replyContent=myBagReply(replyContent,receiveMsg);
        }
        return replyContent;
    }

    /** 
    * @Description: 我的背包
    * @params:  * @param replyContent
    * @param receiveMsg
    * @return: {@link java.lang.String}
    * @Author: lvyq
    * @Date: 2023/3/31 20:23
    */
    private String myBagReply(String replyContent, ReceiveMsg receiveMsg) {
        List<MyBag> myBagList = myBagRepository.findByOpenId(receiveMsg.getFromUserName());
        String myBagStr="";
        myBagStr=myGoodsChange(0,replyContent,myBagList);
        //获取用户
        User user = userRepository.findByOpenId(receiveMsg.getFromUserName());
        replyContent=replyContent.replace("{mybag}",myBagStr).replace("{jinbi}",user.getJinbi().toString());
        return replyContent;
    }

    /** 
    * @Description: 昵称修改
    * @params:  * @param 
    * @return: 
    * @Author: lvyq
    * @Date: 2023/3/23 18:44
    */
    public String nickNameReply(String replyContent, ReceiveMsg receiveMsg){
        //修改昵称#不要喷香水
        String[] nickNames = receiveMsg.getContent().split("#");
        if (nickNames.length==2){
            String  nickName = nickNames[1];
            User user = userRepository.findByOpenId(receiveMsg.getFromUserName());
            user.setNickName(nickName);
            mongoTemplate.save(user);
        }else {
            replyContent="修改昵称失败";
        }
        return replyContent;
    }

    /** 
    * @Description: 签到指令回复
    * @params:  * @param replyContent
    * @param receiveMsg
    * @return: 
    * @Author: lvyq
    * @Date: 2023/3/23 18:34
    */
    public String signReply(String replyContent, ReceiveMsg receiveMsg) {
        User user = userRepository.findByOpenId(receiveMsg.getFromUserName());
        if (user.getIsSignIn()){
            replyContent="您今日已经签到";
        }else {
            Integer jingyan=100;
            Integer jinbi=100;
            //进行替换
            replyContent=replyContent.replace("{jingyanVal}",jingyan.toString())
                                      .replace("{jinbi}",jinbi.toString());
            //修改当前用户的签到状态
            user.setIsSignIn(true);
            user.setJingyan(100+user.getJingyan());
            user.setJinbi(100+user.getJinbi());
            //TODO 爵位判断
            //获取爵位
            List<JueWei> jueWeiList = mongoTemplate.findAll(JueWei.class);
            List<Integer> jingyans = new ArrayList<>();
            for (JueWei jueWei:jueWeiList){
                jingyans.add(jueWei.getJingYanVal());
            }
            jueWeiCheck(jingyans,user);
            mongoTemplate.save(user);
        }
        return replyContent;
    }

    /**
    * @Description: 个人中心指令回复
    * @params:  * @param receiveMsg
    * @return: 
    * @Author: lvyq
    * @Date: 2023/3/23 17:50
    */
    public String userInfoReply(String replyContent,ReceiveMsg receiveMsg) {
        User user = userRepository.findByOpenId(receiveMsg.getFromUserName());
        JueWei jueWei = jueWeiRepository.findByDengJi(user.getDengJi()+1);
        replyContent=replyContent.replace("{nickName}",user.getNickName())
                .replace("{juewei}",user.getJuewei())
                //经验： 200/500
                .replace("{jingyan}",user.getJingyan().toString()+"/"+jueWei.getJingYanVal())
                .replace("{jinbi}",user.getJinbi().toString())
                .replace("{meili}",user.getMeili().toString());
        return replyContent;
    }


    /**
     * @Description: 爵位判断
     * @params:  * @param jingyans
     * @param user
     * @return:
     * @Author: lvyq
     * @Date: 2023/3/21 20:47
     */
    public  void jueWeiCheck(List<Integer> jingyans, User user) {
        for (int i=0;i<jingyans.size();i++){
            if (user.getJingyan()>=jingyans.get(i) && user.getJingyan()<jingyans.get(i+1)){
                JueWei jueWei = jueWeiRepository.findByDengJi(i);
                user.setDengJi(i);
                user.setJuewei(jueWei.getName());
            }
        }
    }


    /** 
    * @Description: 我的背包转换
    * @params:  * @param i
    * @param replyContent
    * @return: {@link java.lang.String}
    * @Author: lvyq
    * @Date: 2023/3/31 20:24
    */
    public String myGoodsChange(int i,String replyContent,List<MyBag>  myBags){
        StringBuffer stringBuffer = new StringBuffer();
        for (MyBag myBag:myBags){
            i=i+1;
            if (i>2){
                i=1;
            }
            if (i == 1) {
                stringBuffer.append(myBag.getGoodsName()+"*"+myBag.getNum()+"|");
            }
            if (i==2){
                stringBuffer.append(myBag.getGoodsName()+"*"+myBag.getNum()+"\n");
            }
        }
        replyContent=stringBuffer.toString();
        return replyContent;
    }
}
